﻿using MediatR;
using S.Authorization.Common.Dto;
using S.Authorization.Domain.Model.User;
using S.Authorization.Infrastructure.Interface.Role;
using S.Authorization.Infrastructure.Interface.User;
using S.Authorization.Read.API.Application.Command.UserCommand;
using S.Authorization.Read.API.Dto;

namespace S.Authorization.Read.API.Application.CommandHandlers.UserCommandHandler
{

    /// <summary>
    /// 根据用户信息获取
    /// </summary>
    public class UserGetByIdCommandHandler : IRequestHandler<UserGetByIdCommand, UserStrategyDto>
    {
        #region 注入
        private readonly IUserModellRepository repository;
        private readonly IRoleRepository roles;
        private readonly IUserRoleRepository userRole;
        public UserGetByIdCommandHandler(IUserModellRepository repository, IUserRoleRepository userRole, IRoleRepository _role)
        {
            this.repository = repository;
            this.userRole = userRole;
            this.roles = _role;
        }
        #endregion
        public async Task<UserStrategyDto> Handle(UserGetByIdCommand request, CancellationToken cancellationToken)
        {
            var user = await repository.GetAllAsync();
            var role =await roles.GetAllAsync();
            var userrole=await userRole.GetAllAsync();
            var list=(from a in user
                      join b in userrole
                      on a.UserId equals b.UserId
                      join c in role
                      on b.RoleId equals c.RoleId
                      where a.UserId==request.UserId
                      select new UserStrategyDto()
                      {
                          UserId=request.UserId,
                          UserTrueName=a.UserTrueName,
                          RoleId=b.RoleId,
                          RoleName=c.RoleName,
                      }).FirstOrDefault();
            return new UserStrategyDto()
            {
                UserId = list.UserId,
                UserTrueName = list.UserTrueName,
                RoleId=list.RoleId,
                RoleName=list.RoleName,
            };
        }
    }
}
